package com.elem.service.impl;

import com.elem.bean.Business;
import com.elem.bean.Cart;
import com.elem.bean.Food;
import com.elem.dao.BusinessDao;
import com.elem.dao.CartDao;
import com.elem.dao.FoodDao;
import com.elem.dao.impl.BusinessDaoImpl;
import com.elem.dao.impl.CartDaoImpl;
import com.elem.dao.impl.FoodDaoImpl;
import com.elem.service.CartService;

import java.util.Collections;
import java.util.List;

public class CartServiceImpl implements CartService {

    // 创建CartDao对象
    CartDao cartDao = new CartDaoImpl();
    // 创建FoodDao对象
    FoodDao foodDao = new FoodDaoImpl();
    // 创建BusinessDao对象
     BusinessDao businessDao = new BusinessDaoImpl();


    @Override
    public List<Cart> listCart(Cart cart) throws Exception {
        List<Cart> cartList = cartDao.listCart(cart);
        for (Cart c : cartList) {
            Food food = foodDao.getFoodById(c.getFoodId());
            if (food != null) {
                c.setFood(food);
            }
            Business business = businessDao.getBusinessById(cart.getBusinessId());
            if (business != null) {
                c.setBusiness(business);
            }
        }
        return cartList;
    }

    @Override
    public int saveCart(Cart cart) throws Exception {
        cart.setQuantity(1); // 默认数量为1
        int i = cartDao.saveCart(cart);
        return i;
    }

    @Override
    public int removeCart(Cart cart) throws Exception {
        int i = cartDao.removeCart(cart);
        return i;
    }

    @Override
    public int updateCart(Cart cart) throws Exception {
        int i = cartDao.updateCart(cart);
        return i;
    }
}
